home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Edit List and Spin / EnumerateEnumerationCombo / EnumerateEnumerationCombo.cs next >
Encoding:
Text File  |  2001-01-15  |  6.5 KB  |  172 lines

  1. //--------------------------------------------------------
  2. // EnumerateEnumerationCombo.cs ⌐ 2001 by Charles Petzold
  3. //--------------------------------------------------------
  4. using Microsoft.Win32;
  5. using System;
  6. using System.Drawing;
  7. using System.Windows.Forms;
  8.  
  9. class EnumerateEnumerationCombo: Form
  10. {
  11.      CheckBox cbHex;
  12.      ComboBox comboLibrary, comboNamespace, comboEnumeration;
  13.      TextBox  tbOutput;
  14.  
  15.      const string strRegKeyBase = 
  16.           "Software\\ProgrammingWindowsWithCSharp\\EnumerateEnumerationCombo";
  17.  
  18.      public static void Main()
  19.      {
  20.           Application.Run(new EnumerateEnumerationCombo());
  21.      }
  22.      public EnumerateEnumerationCombo()
  23.      {
  24.           Text = "Enumerate Enumeration (Combo)";
  25.           ClientSize = new Size(242, 164);
  26.  
  27.           Label label    = new Label();
  28.           label.Parent   = this;
  29.           label.Text     = "Library:";
  30.           label.Location = new Point(8, 8);
  31.           label.Size     = new Size(56, 8);
  32.  
  33.           comboLibrary               = new ComboBox();
  34.           comboLibrary.Parent        = this;
  35.           comboLibrary.DropDownStyle = ComboBoxStyle.DropDown;
  36.           comboLibrary.Sorted        = true;
  37.           comboLibrary.Location      = new Point(64, 8);
  38.           comboLibrary.Size          = new Size(120, 12);
  39.           comboLibrary.Anchor       |= AnchorStyles.Right;
  40.           comboLibrary.TextChanged  += 
  41.                          new EventHandler(ComboBoxLibraryOnTextChanged);
  42.  
  43.           label          = new Label();
  44.           label.Parent   = this;
  45.           label.Text     = "Namespace:";
  46.           label.Location = new Point(8, 24);
  47.           label.Size     = new Size(56, 8);
  48.  
  49.           comboNamespace               = new ComboBox();
  50.           comboNamespace.Parent        = this;
  51.           comboNamespace.DropDownStyle = ComboBoxStyle.DropDown;
  52.           comboNamespace.Sorted        = true;
  53.           comboNamespace.Location      = new Point(64, 24);
  54.           comboNamespace.Size          = new Size(120, 12);
  55.           comboNamespace.Anchor       |= AnchorStyles.Right;
  56.           comboNamespace.TextChanged  +=
  57.                          new EventHandler(ComboBoxNamespaceOnTextChanged);
  58.           
  59.           label          = new Label();
  60.           label.Parent   = this;
  61.           label.Text     = "Enumeration:";
  62.           label.Location = new Point(8, 40);
  63.           label.Size     = new Size(56, 8);
  64.  
  65.           comboEnumeration               = new ComboBox();
  66.           comboEnumeration.Parent        = this;
  67.           comboEnumeration.DropDownStyle = ComboBoxStyle.DropDown;
  68.           comboEnumeration.Sorted        = true;
  69.           comboEnumeration.Location      = new Point(64, 40);
  70.           comboEnumeration.Size          = new Size(120, 12);
  71.           comboEnumeration.Anchor       |= AnchorStyles.Right;
  72.           comboEnumeration.TextChanged  +=
  73.                          new EventHandler(ComboBoxEnumerationOnTextChanged);
  74.  
  75.           cbHex                 = new CheckBox();
  76.           cbHex.Parent          = this;
  77.           cbHex.Text            = "Hex";
  78.           cbHex.Location        = new Point(192, 25);
  79.           cbHex.Size            = new Size(40, 8);
  80.           cbHex.Anchor          = AnchorStyles.Top | AnchorStyles.Right;
  81.           cbHex.CheckedChanged += new EventHandler(CheckBoxOnCheckedChanged);
  82.  
  83.           tbOutput            = new TextBox();
  84.           tbOutput.Parent     = this;
  85.           tbOutput.ReadOnly   = true;
  86.           tbOutput.Multiline  = true;
  87.           tbOutput.ScrollBars = ScrollBars.Vertical;
  88.           tbOutput.Location   = new Point(8, 56);
  89.           tbOutput.Size       = new Size(226, 100);
  90.           tbOutput.Anchor     = AnchorStyles.Left | AnchorStyles.Top |
  91.                                 AnchorStyles.Right | AnchorStyles.Bottom;
  92.  
  93.           AutoScaleBaseSize = new Size(4, 8);
  94.  
  95.                // Initialize display.
  96.  
  97.           FillComboBox(comboLibrary, strRegKeyBase);
  98.           UpdateTextBox();
  99.      }
  100.      void ComboBoxLibraryOnTextChanged(object obj, EventArgs ea)
  101.      {
  102.           FillComboBox(comboNamespace, strRegKeyBase + "\\" + 
  103.                                         comboLibrary.Text);
  104.  
  105.           ComboBoxNamespaceOnTextChanged(obj, ea);
  106.      }
  107.      void ComboBoxNamespaceOnTextChanged(object obj, EventArgs ea)
  108.      {
  109.           FillComboBox(comboEnumeration, strRegKeyBase + "\\" +
  110.                                              comboLibrary.Text + "\\" +
  111.                                                   comboNamespace.Text);
  112.  
  113.           ComboBoxEnumerationOnTextChanged(obj, ea);
  114.      }
  115.      void ComboBoxEnumerationOnTextChanged(object obj, EventArgs ea)
  116.      {
  117.           UpdateTextBox();
  118.      }
  119.      void CheckBoxOnCheckedChanged(object obj, EventArgs ea)
  120.      {
  121.           UpdateTextBox();
  122.      }
  123.      void UpdateTextBox()
  124.      {
  125.           if (EnumerateEnumeration.FillTextBox(tbOutput, comboLibrary.Text, 
  126.                comboNamespace.Text, comboEnumeration.Text, cbHex.Checked))
  127.           {
  128.                if (!comboLibrary.Items.Contains(comboLibrary.Text))
  129.                     comboLibrary.Items.Add(comboLibrary.Text);
  130.  
  131.                if (!comboNamespace.Items.Contains(comboNamespace.Text))
  132.                     comboNamespace.Items.Add(comboNamespace.Text);
  133.  
  134.                if (!comboEnumeration.Items.Contains(comboEnumeration.Text))
  135.                     comboEnumeration.Items.Add(comboEnumeration.Text);
  136.  
  137.                string strRegKey = strRegKeyBase + "\\" + 
  138.                                         comboLibrary.Text + "\\" +
  139.                                              comboNamespace.Text + "\\" +
  140.                                                   comboEnumeration.Text;
  141.                RegistryKey regkey = 
  142.                               Registry.CurrentUser.OpenSubKey(strRegKey);
  143.  
  144.                if (regkey == null)
  145.                     regkey = Registry.CurrentUser.CreateSubKey(strRegKey);
  146.  
  147.                regkey.Close();
  148.           }
  149.      }
  150.      bool FillComboBox(ComboBox combo, string strRegKey) 
  151.      {
  152.           combo.Items.Clear();
  153.  
  154.           RegistryKey regkey = Registry.CurrentUser.OpenSubKey(strRegKey);
  155.  
  156.           if (regkey != null)
  157.           {
  158.                string[] astrSubKeys = regkey.GetSubKeyNames();
  159.                regkey.Close();
  160.  
  161.                if (astrSubKeys.Length > 0)
  162.                {
  163.                     combo.Items.AddRange(astrSubKeys);
  164.                     combo.SelectedIndex = 0;
  165.                     return true;
  166.                }
  167.           }
  168.           combo.Text = "";
  169.           return false;
  170.      }
  171. }
  172.